<?php

class Nimblecms_Controller_Plugin_Navigation extends Zend_Controller_Plugin_Abstract {
	
	public function preDispatch(Zend_Controller_Request_Abstract $request) {
		$containers = array();
		
		$modules = Nimblecms_Module_Loader::getInstance()->getModuleNames();
		
		foreach ($modules as $module) {
			$container = $this->_getContainer($module);
			
			$containers = array_merge($containers, $container);
		}
		
		/*$viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
		if (null === $viewRenderer->view) {
			$viewRenderer->initView();
		}
		$view = $viewRenderer->view;
		$view->menu = new Zend_Navigation($containers);*/
		Zend_Registry::set('container',$containers);
	}
	
	/**
	 * Получает контейнер модуля для навигации
	 */
	private function _getContainer($name) {
		$dir = APPLICATION_PATH . DS . 'modules' . DS . $name . DS . 'service';
		
		if(!is_dir($dir)) {
			return array();
		}
		
		$file = $dir . DS . 'Navigation.php';
		
		if(!file_exists($file)) {
			return array();
		}
		
		$className = ucfirst($name) . '_Service_Navigation';
		
		$object = new $className();
		
		$container = $object->getContainer();
		
		return $container;
	}
}

?>